﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using System.Xml.Linq;
using HandyControl.Controls;
using SharpDX;
using System.Windows.Input;
using System.Windows.Threading;

namespace WpfModelEditor.Core.Controls
{
    [TemplatePart(Name = "PART_Popup", Type = typeof(Popup))]
    [TemplatePart(Name = "PART_COLOR", Type = typeof(ColorPicker))]
    public class Color4Picker : Control
    {
        private const string ElementPicker = "PART_COLOR";
        private ColorPicker _picker;


        public bool IsDropDownOpen
        {
            get { return (bool)GetValue(IsDropDownOpenProperty); }
            set { SetValue(IsDropDownOpenProperty, value); }
        }

        public static readonly DependencyProperty IsDropDownOpenProperty = DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(Color4Picker), new PropertyMetadata(false, OnIsDropDownOpenChanged));

        private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var cp = (Color4Picker)d;
            var flag = (bool)e.NewValue;
            if (cp._picker != null)
            {
                if (flag)
                {
                    cp._picker.SelectedBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromScRgb(cp.SelectedValue.Alpha, cp.SelectedValue.Red, cp.SelectedValue.Green, cp.SelectedValue.Blue));
                }
                else
                {
                    cp.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        Mouse.Capture(null);
                    }), DispatcherPriority.Send);
                }
            }
        }

        public Color4 SelectedValue
        {
            get { return (Color4)GetValue(SelectedValueProperty); }
            set { SetValue(SelectedValueProperty, value); }
        }

        public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(Color4), typeof(Color4Picker), new PropertyMetadata(Color4.Black, OnSelectedValueChanged));

        private static void OnSelectedValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var cp = (Color4Picker)d;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _picker = GetTemplateChild(ElementPicker) as ColorPicker;
            _picker.Confirmed += _picker_Confirmed;
            _picker.Canceled += _picker_Canceled;
        }

        private void _picker_Canceled(object sender, EventArgs e)
        {
            IsDropDownOpen = false;
        }

        private void _picker_Confirmed(object sender, HandyControl.Data.FunctionEventArgs<System.Windows.Media.Color> e)
        {
            SelectedValue = new Color4(_picker.SelectedBrush.Color.R / 255f, _picker.SelectedBrush.Color.G / 255f, _picker.SelectedBrush.Color.B / 255f, _picker.SelectedBrush.Color.A / 255f);
            IsDropDownOpen = false;
        }
    }
}
